﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace SerialPortTest
{
    public partial class MainFrame : Form
    {
        private SerialPort sp = null;
        private String tmpStr = null;
        private AutoResetEvent autoEvent = new AutoResetEvent(false);
        public delegate void DG();
        public MainFrame()
        {
            InitializeComponent();
        }

        private void MainFrame_Load(object sender, EventArgs e)
        {

            open();
        }

        private void open()
        {
                var tmpSp = sp;
                sp = null;
                if (tmpSp != null)
                {

                    tmpSp.Close();
                    tmpSp.Dispose();
                }


                tmpSp = new SerialPort();
                tmpSp.PortName = "com6";
                tmpSp.BaudRate = 9600;
                tmpSp.Open();
                tmpSp.ReadTimeout = 1000;
                tmpSp.RtsEnable = true;
                tmpSp.DataReceived += sp_DataReceived;

                sp = tmpSp;
                Console.WriteLine("打开串口");


        }

        private void sendSms(String phone, String msg)
        {


        }

        private  String sendAt(String str)
        {
            if (sp == null)
            {
                return "串口还没有打开";
            }

            lock (sp)
            {

                autoEvent = new AutoResetEvent(false);
                sp.Write(str + "\r");
                var b = autoEvent.WaitOne(30000);
                if (b)
                {
                    return tmpStr;
                }
                else
                {
                    open();
                    return "超时";
                }

            }
        }

        private void sp_DataReceived(object sender, EventArgs e)
        {
            StringBuilder tmp = new StringBuilder();
            while (true)
            {
                Thread.Sleep(100);
                String t = null;
                try
                {
                    t = sp.ReadExisting();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("异常:" + ex.Message);
                    break;
                }
                Console.Write(t);
                if (t == "")
                {
                    break;
                }
                tmp.Append(t);
            }

            if (tmp.ToString().Contains("OK") || tmp.ToString().Contains("ERROR"))
            {
                tmpStr = tmp.ToString();
                autoEvent.Set();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            String at = textBox2.Text;
            new Thread(delegate() {
                String str = sendAt(at);

                DG dg = delegate()
                {
                    textBox1.AppendText(at + "\r\n----\r\n" + str + "\r\n-------------------------------------\r\n");
                };

                Invoke(dg);
            
            }).Start();




        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1_Click(null, null);
            }
        }





    }
}
